#include "mywidget.h"
#include "ui_mywidget.h"

myWidget::myWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::myWidget)
{
    ui->setupUi(this);
    setWindowTitle("微信关注“程序媛讲QT”");

    movie=new QMovie(this);
    movie->setCacheMode(QMovie::CacheAll);

    movie_label=new QLabel(this);
    movie_label->setText("No Animated Picture to Display");
    movie_label->setAlignment(Qt::AlignCenter);   //QLabel内容居中
    movie_label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);   //布局属性
    movie_label->setBackgroundRole(QPalette::Shadow);   //背景样式
    movie_label->setAutoFillBackground(true);   //自动填充

    create_MovieControls();
    create_MovieBtns();

    connect(movie,&QMovie::frameChanged,this,&myWidget::updateframe);

    //布局
    main_layout = new QVBoxLayout;
    main_layout->addWidget(movie_label);
    main_layout->addLayout(moviecontrols_layout);
    main_layout->addLayout(moviebtns_layout);
    this->setLayout(main_layout);


}


void myWidget::updateframe()
{
    bool hasFrames = (movie->currentFrameNumber() >= 0);
    if(hasFrames)
    {
        if(movie->frameCount()>0)
        {
            Progress_slider->setMaximum(movie->frameCount()-1);
        }
        else
        {
            if (movie->currentFrameNumber() > Progress_slider->maximum())
                 Progress_slider->setMaximum(movie->currentFrameNumber());
        }
        Progress_slider->setValue(movie->currentFrameNumber());
    }
    else
    {
        Progress_slider->setMaximum(0);
    }

}




void myWidget::gotoFrame(int frame)
{
    movie->jumpToFrame(frame);
}





void myWidget::create_MovieControls()
{
    Progress_label=new QLabel(this);
    Progress_label->setText("Current progress:");
    Progress_slider=new QSlider(this);
    Progress_slider->setOrientation(Qt::Horizontal);
    Progress_slider->setEnabled(false);
    connect(Progress_slider,&QSlider::valueChanged,this,&myWidget::gotoFrame);

    //布局
    moviecontrols_layout= new QHBoxLayout;
    moviecontrols_layout->setContentsMargins(20,2,40,2);
    moviecontrols_layout->addWidget(Progress_label);
    moviecontrols_layout->addWidget(Progress_slider);

}



void myWidget::openfile_btn_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, "Choose a GIF picture", "../",tr("Images (*.gif)"));
    if (!fileName.isEmpty())
    {
        movie->stop();
        movie_label->setMovie(movie);   //将QMovie对象传递给QLabel::setMovie()
        movie->setFileName(fileName );

        if(movie->isValid() && movie->frameCount() != 1&& movie->state() == QMovie::NotRunning)
        {
                movie->start();
                btn_setpause();
                Progress_slider->setEnabled(true);
                Stop_btn->setEnabled(true);
                PlayPause_btn->setEnabled(true);
                Fitwindow_box->setEnabled(true);
                Speed_box->setEnabled(true);
        }

    }

}


void myWidget::speedbox_value_changed()
{
   float moviespeed= Speed_box->value();
   moviespeed=moviespeed*100;
   movie->setSpeed(moviespeed);    //设置movie播放速度
}


void myWidget::playpause_btn_clicked()
{
    if(isRun==false)
    {
        movie->setPaused(false);
        btn_setpause();
    }
    else
    {
        movie->setPaused(true);
        btn_setrun();
    }

}



void myWidget::create_MovieBtns()
{
    Openfile_btn=new QPushButton(this);
    Openfile_btn->setFixedSize(35,30);
    QIcon btn_icon;
    btn_icon.addFile(":/new/prefix1/image/file.png");
    Openfile_btn->setIcon(btn_icon);
    Openfile_btn->setIconSize(QSize(30, 30));
    connect(Openfile_btn,&QPushButton::clicked,this,&myWidget::openfile_btn_clicked);

    Stop_btn=new QPushButton(this);
    Stop_btn->setFixedSize(35,30);
    QIcon btn_icon1;
    btn_icon1.addFile(":/new/prefix1/image/stop.png");
    Stop_btn->setIcon(btn_icon1);
    Stop_btn->setIconSize(QSize(32, 32));
    Stop_btn->setEnabled(false);
    connect(Stop_btn,&QPushButton::clicked,
            [=]()
            {
                movie->stop();
                btn_setrun();
            });


    PlayPause_btn=new QPushButton(this);
    PlayPause_btn->setFixedSize(35,30);
    btn_setrun();
    PlayPause_btn->setEnabled(false);
    connect(PlayPause_btn,&QPushButton::clicked,this,&myWidget::playpause_btn_clicked);


    Fitwindow_box=new QCheckBox(this);
    Fitwindow_box->setText("Fit to Window");
    Fitwindow_box->setFixedSize(110,30);
    Fitwindow_box->setEnabled(false);
    connect(Fitwindow_box,&QCheckBox::clicked,this,&myWidget::gif_fitwindow);

    Speed_label=new QLabel(this);
    Speed_label->setText("Speed:");
    Speed_box=new QDoubleSpinBox(this);
    Speed_box->setDecimals(1);    //SpinBox显示一位小数点
    Speed_box->setRange(0.1,5);  //SpinBox 数值范围
    Speed_box->setSingleStep(0.1);  //单步size
    Speed_box->setValue(1);
    Speed_box->setEnabled(false);
    connect(Speed_box,static_cast<void (QDoubleSpinBox::*)(double index)>(&QDoubleSpinBox::valueChanged),this, &myWidget::speedbox_value_changed);


    //布局
    moviebtns_layout= new QHBoxLayout;
    moviebtns_layout->setContentsMargins(20,2,10,2);
    moviebtns_layout->addWidget(Openfile_btn);
    moviebtns_layout->addWidget(PlayPause_btn);
    moviebtns_layout->addWidget(Stop_btn);
    moviebtns_layout->addStretch();
    moviebtns_layout->addWidget(Speed_label);
    moviebtns_layout->addWidget(Speed_box);
    moviebtns_layout->addStretch();
    moviebtns_layout->addWidget(Fitwindow_box);
    moviebtns_layout->addStretch();



}


void myWidget::gif_fitwindow()
{
    movie_label->setScaledContents(Fitwindow_box->isChecked());
}



void myWidget::btn_setrun()
{
    isRun=false;
    btn_icon2.addFile(":/new/prefix1/image/start.png");
    PlayPause_btn->setIcon(btn_icon2);
    PlayPause_btn->setIconSize(QSize(17, 17));
}


void myWidget::btn_setpause()
{
    isRun=true;
    btn_icon2.addFile(":/new/prefix1/image/pause.png");
    PlayPause_btn->setIcon(btn_icon2);
    PlayPause_btn->setIconSize(QSize(17, 17));
}


myWidget::~myWidget()
{
    delete ui;
}








